﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Alert_Form.Component.Alert;
using YXChatFormClient.Dtos.Account;
using YXChatFormClient.Global;
using YXChatFormClient.Utility;
using YXChatFormClient.Utility.HubHelp;

namespace YXChatFormClient
{
    public partial class FrmLogin : Form
    {
        private HttpHelper _http;

        public FrmLogin()
        {
            Task.Run(() =>
            {
                _http = new HttpHelper();
            });
            InitializeComponent();
            Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, this.Width, this.Height, 15, 15));
            this.lbDesc.Text = JSONHelper.ReadJSON<string>("LoginTitle");
            AutoSetValue();
        }
        private void AutoSetValue()
        {
            var accountNumber = JSONHelper.ReadJSON<string>("AccountNumber");
            var passWord = JSONHelper.ReadJSON<string>("PassWord").ToString();
            this.tbAccountName.Text = accountNumber;
            // 有密码代表有记住密码
            if (!accountNumber.IsNullOrWhiteSpace() && !passWord.IsNullOrWhiteSpace())
            {
                this.tbAccountPwd.Text = passWord;
                this.ckRememberPwd.Checked = true;
                if (JSONHelper.ReadJSON<bool>("IsAutoLogin"))
                {
                    this.ckAutoLogin.Checked = true;
                    Task.Run(async () =>
                    {
                        await Task.Delay(1000);
                        btLogin_Click(null, null);
                    });
                }
            }
            
        }
        private void picClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void picClose_MouseMove(object sender, MouseEventArgs e)
        {
            picClose.BackColor = Color.FromArgb(250, 81, 81);
            picClose.ImageLocation = ImageHelper.GetStaticImage("close_active.png");
        }

        private void picClose_MouseLeave(object sender, EventArgs e)
        {
            picClose.BackColor = Color.White;
            picClose.ImageLocation = ImageHelper.GetStaticImage("close.png");
        }

        #region 边框阴影
        private const int CS_DropShadow = 0x00020000;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= CS_DropShadow;
                return cp;
            }
        }
        #endregion
        #region 自定义拖动窗体
        [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
        private static extern void ReleaseCapture();

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private static extern void SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
        #endregion
        #region 圆角
        private void panelHearder_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, 0x112, 0xf012, 0);
        }
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
        #endregion

        private void lbRegister_Click(object sender, EventArgs e)
        {
            FrmRegister frmRegister = new FrmRegister();
            frmRegister.ShowDialog();
        }

        private async void btLogin_Click(object sender, EventArgs e)
        {
            LoginInput input = new LoginInput
            {
                AccountNumber = tbAccountName.Text,
                PassWord = tbAccountPwd.Text,
            };
            var account = await _http.PostAsync<AccountDto>(Api.AccountLogin, input);
            if (account != null)
            {
                GlobalAccountInfo.Account = account;
                GlobalAccountInfo.AccountAvatar = account.Avatar;
                ChatService.NotifyServiceAddClient(account.AccountNumber);
                JSONHelper.WriteJSON("AccountNumber", tbAccountName.Text);
                JSONHelper.WriteJSON("IsRememberPwd", ckRememberPwd.Checked);
                if (ckRememberPwd.Checked)
                {
                    JSONHelper.WriteJSON("PassWord", tbAccountPwd.Text);
                    if (ckAutoLogin.Checked)
                    {
                        JSONHelper.WriteJSON("IsAutoLogin", ckAutoLogin.Checked);
                    }
                }
                else
                {
                    JSONHelper.WriteJSON("PassWord","");
                }
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                AlertForm.ShowAlert("账号或密码错误", AlertType.Error);
            }
        }

        private void ckAutoLogin_CheckedChanged(object sender, EventArgs e)
        {
            if (!ckRememberPwd.Checked)
            {
                ckRememberPwd.Checked = true;
            }
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            
        }
    }
}
